from py2neo import Graph, Node, Relationship
from py2neo.matching import NodeMatcher, RelationshipMatcher


graph = Graph("bolt://localhost:7687", auth=("neo4j", "neo5j"))
matcher = NodeMatcher(graph)
# Find all nodes with label Movie
movies = matcher.match("Movie")
# Find all nodes with label Genre
genres = matcher.match("Genre")
# Print all genres
for genre in genres:
    print(genre, genre["name"])
# Find all nodes with label Director
directors = matcher.match("Director")
# Find all nodes with label Actor
actors = matcher.match("Actor")

print('-'*50)
# Find all nodes with label Movie and name "肖申克的救赎"
movie = matcher.match("Movie", name="肖申克的救赎").first()
print(movie, movie['name'])
# Find all nodes with label Genre and name "剧情"
genre = matcher.match("Genre", name="剧情").first()
print(genre, genre['name'])
# Find all nodes with label Director and name "弗兰克·德拉邦特"
director = matcher.match("Director", name="弗兰克·德拉邦特").first()
print(director, director['name'])
# Find all nodes with label Actor and name "蒂姆·罗宾斯"
actor = matcher.match("Actor", name="蒂姆·罗宾斯").first()
print(actor, actor['name'])

print('-'*50)
# Find all movies with name "哈利·波特"
harry_potter = matcher.match("Movie").where("_.name =~ '.*哈利.*.波特.*'")
print(list(harry_potter))
# Find all movies with score greater than 9.0
high_score = matcher.match("Movie").where("_.star >= 9.6")
print(list(high_score))
# Find all movies with releaseTime earlier than 1970
old_movie = matcher.match("Movie").where("_.releaseTime < date('1970-01-01')")
print(list(old_movie))


print('-'*50)
relationship_matcher = RelationshipMatcher(graph)
titanic = matcher.match("Movie", name="泰坦尼克号").first()
print(titanic['name'])
relationship = relationship_matcher.match(nodes=[titanic], r_type="属于")
print(list(relationship))
relationship = relationship_matcher.match([titanic], r_type=None)
print(list(relationship))

print('-'*50)
relationship = relationship_matcher.match(None, r_type='导演')
print(list(relationship))
